
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 1e3+5;
const int INF = 2e6;
int dp[maxn][3];
int x[maxn], y[maxn], val[maxn], book[maxn];
vector<int> p[maxn];
void dfs(int u)
{
    dp[u][0] = 0;
    dp[u][1] = val[u];
    book[u] = 1;
    for(int i = 0; i < p[u].size(); i++)
    {
        int to = p[u][i];
        if(book[to]) continue;
        dfs(to);
        dp[u][1] += max(dp[to][1],0);  
        dp[u][0] = max(dp[u][0], max(dp[to][1], dp[to][0]));  
}
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        memset(book, 0, sizeof(book));
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++)
            p[i].clear();
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &x[i], &y[i], &val[i]);
        }
        for(int i = 1; i  <= n; i++)
            for(int j = i+1; j <= n; j++)
            {
                if(abs(x[i]-x[j])+abs(y[i]-y[j]) == 1)
                {
                    p[i].push_back(j);  
                    p[j].push_back(i);
                }
            }
        dfs(1);
        printf("%d\n", max(dp[1][0], dp[1][1]));
    }
    return 0;
}

